package com.conwax.gmap;

import org.apache.wicket.behavior.IBehavior;
import org.apache.wicket.markup.html.panel.Panel;

import com.conwax.gmap.DivElementContainer;
import com.conwax.gmap.GLatLng;
import com.conwax.gmap.JsApiBehavior;
import com.conwax.gmap.Map2;
import com.conwax.gmap.MapsApiBehavior;

/**
 */
public class Map2Panel extends Panel
{

	private static final long serialVersionUID = 1L;
	private final DivElementContainer divElement;
	private final Map2 map;

	public Map2Panel(String id)
	{
		super(id);
		add(divElement = new DivElementContainer("divElementId"));
		map = new Map2(divElement);
	}

	public void setCenter(GLatLng latLng, int i)
	{
		map.setCenter(latLng, i);
		
	}

	@Override
	protected void onBeforeRender()
	{
		if (getIBehavior(JsApiBehavior.class)  == null)
		{
			getPage().add(new JsApiBehavior("ljaksdf"));
		}

		MapsApiBehavior mapsApiBehavior = getIBehavior(MapsApiBehavior.class); 
		if (mapsApiBehavior == null)
		{
			getPage().add(mapsApiBehavior = new MapsApiBehavior());
		}

//		mapsApiBehavior.add(map);

		super.onBeforeRender();
	}

	private <M extends IBehavior> M getIBehavior(Class<M> type)
	{
		for (IBehavior behavior : getPage().getBehaviors())
		{
			if (type.isAssignableFrom(behavior.getClass()))
			{
				return (M) behavior;
			}
		}
		return null;
	}
}
